//
//  LoginViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/15.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "LoginViewController.h"

#import "LoginKJViewController.h"
#import "RigisterViewController.h"
#import "ResetPwdViewController.h"
#import "AppDelegate.h"
@interface LoginViewController ()<LoginKJViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textNum;
@property (weak, nonatomic) IBOutlet UITextField *textPwd;
@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UIButton *btnSavePwd;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadNav];
    [self loadUI];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _textNum.text = [user objectForKey:@"Loginphone"];
    _textPwd.text = [user objectForKey:@"Loginpwd"];
    NSLog(@"pwd:%@",_textPwd.text);
}

- (void)loadNav{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    [line makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(0.5);
    }];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"登录";
    lab.textColor = [UIColor blackColor];
    lab.font = [UIFont systemFontOfSize:18.0];
    [view addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(34);
    }];
    
    //返回按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.left.equalTo(10);
        make.width.equalTo(30);
        make.height.equalTo(30);
    }];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setTitle:@"注册" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(loadZhuCe) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn2];
    [btn2 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.right.equalTo(-10);
        make.width.equalTo(40);
        make.height.equalTo(30);
    }];
}

- (void)clickBack{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UITabBarController *vc = delegate.tabbar;
    NSInteger tags = [[user objectForKey:@"item"] integerValue];
    vc.selectedIndex = tags;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loadZhuCe{
    RigisterViewController *rigister = [[RigisterViewController alloc] init];
    [self.navigationController pushViewController:rigister animated:YES];
}

- (void)loadUI{
    _view1.layer.borderWidth = 1.0;
    _view1.layer.borderColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0].CGColor;
    
    _view2.layer.borderWidth = 1.0;
    _view2.layer.borderColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0].CGColor;
    
}
//记住密码
- (IBAction)btnJiZhu:(UIButton *)sender {
    if (sender.tag == 10) {
        [sender setImage:[UIImage imageNamed:@"勾-(1)"] forState:UIControlStateNormal];
        sender.tag = 20;
        _btnSavePwd.tag = 20;
    }else{
        [sender setImage:[UIImage imageNamed:@"矩形-7"] forState:UIControlStateNormal];
        sender.tag = 10;
        _btnSavePwd.tag = 10;
    }
}

//快捷登录
- (IBAction)btnkjLogin:(id)sender {
    LoginKJViewController *kjLogin = [[LoginKJViewController alloc] init];
    kjLogin.delegate = self;
    [self.navigationController pushViewController:kjLogin animated:YES];
}

-(void)send:(NSString *)text{
    if ([text isEqualToString:@"suc"]) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

//登录
- (IBAction)btnLogin:(id)sender {
    NSLog(@"tag:%li",_btnSavePwd.tag);
    //20,记住密码，10否
    if ([self validateMobile:_textNum.text] == 0) {
        [LICGToast showWithText:@"手机号码格式不正确"];
        
        return;
    }else if (_textPwd.text.length == 0){
        [LICGToast showWithText:@"密码格式不正确"];
        
        return;
    }
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmars = [NSMutableDictionary dictionary];
    [parmars setObject:_textNum.text forKey:@"mobilephone"];
    [parmars setObject:_textPwd.text forKey:@"password"];
    [parmars setObject:@"2" forKey:@"type"];
    NSString *token = [user objectForKey:@"token"] ? [user objectForKey:@"token"] : @"";
    [parmars setObject:token forKey:@"pushtoken"];
    NSLog(@"parmas:%@",parmars);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Userlogin/login",urlHead];
    [manager POST:url parameters:parmars success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"登录res:%@",responseObject);
        
        NSString *state = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        
        NSString *userid = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"userid"] integerValue]];
        NSString *token = [responseObject objectForKey:@"token"];
        
        if ([state isEqualToString:@"200"] ) {
            [user setObject:userid forKey:@"userid"];
            [user setObject:token forKey:@"token2"];
            if (_btnSavePwd.tag == 20) {
                [user setObject:_textNum.text forKey:@"Loginphone"];
                [user setObject:_textPwd.text forKey:@"Loginpwd"];
            }else{
                [user setObject:@"" forKey:@"Loginphone"];
                [user setObject:@"" forKey:@"Loginpwd"];
            }
            
            [LICGToast showWithText:@"登录成功"];
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            [LICGToast showWithText:[responseObject objectForKey:@"msg"]];
        }
        
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

- (IBAction)btnReset:(id)sender {
    ResetPwdViewController *reset = [[ResetPwdViewController alloc] init];
    [self.navigationController pushViewController:reset animated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
